package com.ql.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ql.dao.ProjectDao;
import com.ql.domain.response.JsonResult;
import com.ql.domain.Project;
import com.ql.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    ProjectDao projectDao;


    @Override
    public JsonResult findAll(String projectName, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Project> all = projectDao.findAll(projectName);
        PageInfo pageInfo = new PageInfo(all);
        return JsonResult.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public JsonResult save(Project project) {
        try {
            project.setCreate_time(new Date());
            projectDao.save(project);
            return JsonResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public JsonResult update(Project project) {
        try {
            projectDao.update(project);
            return JsonResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public JsonResult del(String id) {
        try {
            projectDao.delProjectById(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public List<Project> projectGroupName(String[] projectIds) {
        return projectDao.projectGroupName(projectIds);
    }

    @Override
    public List<Project> allProjectGroupName() {
        return projectDao.allProjectGroupName();
    }
}
